/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AjouConquerorBeans;

import Classes.GameRoomClass;
import java.util.ArrayList;
import javax.ejb.Singleton;

/**
 *
 * @author ajou
 */
@Singleton
public class GameRoomBean {

    ArrayList<GameRoomClass> Rooms = new ArrayList<GameRoomClass>();
    GameRoomClass Room;
    int RoomID = 0;

    public GameRoomClass getRoom(int num) {
        for (GameRoomClass g : Rooms) {
            System.out.println("Room id is: " + g.getId());
            if (g.getId() == num) {
                return g;
            }
        }
        return null;
    }

    public String ModifyRoom(int roomid, String userid) {
        for (int i = 0; i < Rooms.size(); i++) {
            if (Rooms.get(i).getId() == roomid) {
                if (Rooms.get(i).getTeam1_M1().equals(userid)) {
                    return "1";
                } else if (Rooms.get(i).getTeam1_M2() == null) {
                    Rooms.get(i).setTeam1_M2(userid);
                    return "1";
                } else if (Rooms.get(i).getTeam2_M1() == null) {
                    Rooms.get(i).setTeam2_M1(userid);
                    return "2";
                } else if (Rooms.get(i).getTeam2_M2() == null) {
                    Rooms.get(i).setTeam2_M2(userid);
                    return "2";
                }
            }
        }
        return "";
    }

    public void removeuser(int roomid, String UserName) {

        for (int i = 0; i < Rooms.size(); i++) {
            if (Rooms.get(i).getId() == roomid) {
                if (Rooms.get(i).getTeam1_M1().equals(UserName)) {
                    Rooms.get(i).setTeam1_M1(null);
                    Rooms.remove(i);
                } else if (Rooms.get(i).getTeam1_M2().equals(UserName)) {
                    Rooms.get(i).setTeam1_M2(null);
                } else if (Rooms.get(i).getTeam2_M1().equals(UserName)) {
                    Rooms.get(i).setTeam2_M1(null);
                } else if (Rooms.get(i).getTeam2_M2().equals(UserName)) {
                    Rooms.get(i).setTeam2_M2(null);
                }

            }
        }
    }

    public int CreateRoom(String RoomName, String UserName) {
        Room = new GameRoomClass();
        Room.setRoomName(RoomName);
        Room.setTeam1_M1(UserName);
        Room.setId(RoomID);
        Rooms.add(Room);

        return RoomID++;
    }

    public String changeteam(String roomid, String UserName) {
        String result = "1";
        for (int i = 0; i < Rooms.size(); i++) {
            GameRoomClass sroom = Rooms.get(i);

            if (sroom.getId() == Integer.parseInt(roomid)) {

                if (sroom.getTeam1_M1().equals(UserName)) {
                    result = "master can not move to other team";
                } else if (sroom.getTeam1_M2() != null && sroom.getTeam1_M2().equals(UserName)) {
                    if (sroom.getTeam2_M1() == null) {
                        sroom.setTeam2_M1(UserName);
                        sroom.setTeam1_M2(null);
                        result = "2";
                    } else if (sroom.getTeam2_M2() == null) {
                        sroom.setTeam2_M2(UserName);
                        sroom.setTeam1_M2(null);
                        result = "2";
                    }
                } else if (sroom.getTeam2_M1() != null && sroom.getTeam2_M1().equals(UserName)) {
                    if (sroom.getTeam1_M2() == null) {
                        sroom.setTeam1_M2(UserName);
                        sroom.setTeam2_M1(null);
                        result = "1";
                    }

                } else if (sroom.getTeam2_M2() != null && sroom.getTeam2_M2().equals(UserName)) {
                    if (sroom.getTeam1_M2() == null) {
                        sroom.setTeam1_M2(UserName);
                        sroom.setTeam2_M2(null);
                        result = "1";
                    }
                }
            }
        }
        return result;
    }

    public String getready(int roomid) {
        String tp = "notready";

        for (int i = 0; i < Rooms.size(); i++) {
            GameRoomClass sroom = Rooms.get(i);
            if (sroom.getId() == roomid) {
                tp = sroom.getRady();
            }
        }
        return tp;
    }

    public void setforReady(int roomid) {
        for (int i = 0; i < Rooms.size(); i++) {
            GameRoomClass sroom = Rooms.get(i);
            if (sroom.getId() == roomid) {
                sroom.setRady("go");
            }
        }
    }

    public ArrayList<GameRoomClass> findallRooms() {
        return Rooms;
    }
}
